package cn.good.yan.service.impl;

import cn.good.yan.entity.Account;
import cn.good.yan.mapper.AccountMapper;
import cn.good.yan.service.AccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * 订单服务
 *
 * @author shengren.yan
 * @create 2024-02-22
 */
@Slf4j
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {

    @Autowired
    AccountMapper accountMapper;

    // 扣除 该账户的 金额
//    @Transactional(rollbackFor = Exception.class)
    @Override
    public void decrease(Long userId, BigDecimal money) {
        log.info("-----> account扣减余额开始");
        // 这里的 写死的id，动态需要写活
        Account account = accountMapper.selectById(userId);
        account.setUsed(account.getUsed() + money.longValue());
        account.setResidue(account.getResidue() - money.longValue());
        accountMapper.updateById(account);
        log.info("-----> account扣减余额结束");
    }


}
